﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using FubiManger.Controls;
using Newtonsoft.Json;
using System.Text.RegularExpressions;

namespace FubiManger.UI
{
    partial class AddactivityForm : Smobiler.Core.Controls.MobileForm
    {
        public AddactivityForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 商户名称
        /// </summary>
        public string merchantname { get; set; }
        /// <summary>
        /// 活动类型
        /// </summary>
        public string activity_type { get; set; }
        ms_merchant merchant = new ms_merchant();
        ms_merchant_activity activity = new ms_merchant_activity();
        /// <summary>
        /// 使用方式
        /// </summary>
        private string usage = "1";
        /// <summary>
        /// 1小时2分钟
        /// </summary>
        private string unit = "1";
        /// <summary>
        /// 1新增2更新
        /// </summary>
        public string stype { get; set; }
        /// <summary>
        /// 活动ID
        /// </summary>
        public string actvid { get; set; }
        /// <summary>
        /// 规格
        /// </summary>
        List<specs> splist = new List<specs>();
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panaddimg_Press(object sender, EventArgs e)
        {
            camera1.GetPhoto();
        }
        /// <summary>
        /// 保存更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void pansave_Press(object sender, EventArgs e)
        {

            if (imglist.Count == 0)
            {
                MessageBox.Show("请上传封面");
            }
            else if (imglist.Count < 2)
            {
                MessageBox.Show("请上传详情图");
            }
            else if (string.IsNullOrEmpty(txtgoodname.Text))
            {
                MessageBox.Show("请填写商品名称");
            }
            else if (string.IsNullOrEmpty(txtdescribes.Text))
            {
                MessageBox.Show("请填写商品描述");
            }
            else if (string.IsNullOrEmpty(txtstock.Text))
            {
                MessageBox.Show("请填写商品库存");
            }
            else if (Convert.ToDouble(txtmarkprice.Text) < 1)
            {
                MessageBox.Show("请填写商品市场价");
            }
            else if (Convert.ToDouble(txtprice.Text) < 1)
            {
                MessageBox.Show("请填写商品售价");
            }
            else if (DateTime.Compare(dpend.Value, dpstart.Value) < 0)
            {
                MessageBox.Show("活动结束时间需大于开始时间");
            }
            else
            {
                string carousel = "";
                for (int i = 1; i < imglist.Count; i++)
                {

                    if (i == 1)
                        carousel = imglist[i];
                    else
                        carousel += "," + imglist[i];
                }
                ms_merchant_activity activity = new ms_merchant_activity();
                activity.merchant_id = merchantid;//商户ID
                activity.merchant_name = merchant.merchant_name;//商户名称
                activity.goods_name = txtgoodname.Text; //商品名称
                activity.cover = imglist[0];//封面
                activity.carousel = carousel;//轮播图
                activity.describes = txtdescribes.Text;
                activity.stock = txtstock.Text;//库存
                activity.start_time = dpstart.Value.ToString("yyyy-MM-dd HH:mm:ss");//开始时间
                activity.end_time = dpend.Value.ToString("yyyy-MM-dd HH:mm:ss");//开始时间
                activity.province = merchant.province;//省份
                activity.u_sage = usage;//使用方式                          
                //activity.often = txtoften.Text;//时长
                activity.activity_type = activity_type;//活动类型
                //if (lbltimer.Text == "小时")
                //{
                //    activity.unit = "1";//时间单位
                //    activity.end_time = Convert.ToDateTime(activity.start_time).AddHours(Convert.ToInt32(txtoften.Text)).ToString("yyyy-MM-dd HH:mm:ss");
                //}
                //else
                //{
                //    activity.unit = "2";//时间单位
                //    activity.end_time = Convert.ToDateTime(activity.start_time).AddMinutes(Convert.ToInt32(txtoften.Text)).ToString("yyyy-MM-dd HH:mm:ss");
                //}

                activity.market_price = txtmarkprice.Text;//市场价
                activity.discount_price = txtprice.Text;//活动价
                try
                {
                    if (stype == "2")
                    {
                        if (merchant_activityDal.Updateactvitybyid(activity, actvid) > 0)
                        {
                            Toast("更新成功");
                            this.Close();
                        }
                    }
                    else
                    {
                        if (merchant_activityDal.Addactivity(activity) > 0)
                        {
                            Pushacticontrol con = new Pushacticontrol();
                            this.ShowDialog(con, (obj, args) =>
                            {
                                this.Close();
                            });
                        }
                    }
                }
                catch (Exception ex)
                {

                   // MessageBox.Show("系统异常");
                    MessageBox.Show(ex.ToString());
                }

            }

        }
        int index = 0;
        List<string> imglist = new List<string>();
        string cover = "";
        private void camera1_ImageCaptured(object sender, BinaryResultArgs e)
        {
            if (e.isError == false)
            {
                string name = DateTime.Now.ToString("yyyyMMddHHmmssms") + Getcode() + ".png";
                e.SaveFile(name, @"C:\fbimage\");
                imglist.Add(name);
                imgcontrol con = new imgcontrol();
                con.cover = @"C:\fbimage\" + name;
                con.index = imglist.Count - 1;
                con.stype = "2";
                panimg.Controls.Add(con);
            }
        }
        /// <summary>
        /// 更新图片
        /// </summary>
        /// <param name="index"></param>
        /// <param name="imgname"></param>
        public void Updateimg(int index, string imgname)
        {
            imglist[index] = imgname;//更新图片
            panimg.Controls.Clear();
            for (int i = 0; i < imglist.Count; i++)
            {
                imgcontrol con1 = new imgcontrol();
                con1.cover = @"C:\fbimage\" + imglist[i];
                con1.index = i;
                con1.stype = "2";
                panimg.Controls.Add(con1);
            }
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="index"></param>
        public void Delimg(int index)
        {
            imglist.RemoveAt(index);
            panimg.Controls.Clear();
            for (int i = 0; i < imglist.Count; i++)
            {
                imgcontrol con1 = new imgcontrol();
                con1.cover = @"C:\fbimage\" + imglist[i];
                con1.index = i;
                con1.stype = "2";
                panimg.Controls.Add(con1);
            }
        }
        /// <summary>
        /// 随机4位数
        /// </summary>
        /// <returns></returns>
        private int Getcode()
        {
            Random rand = new Random();
            return rand.Next(1000, 9999);
        }

        private void AddactivityForm_Load(object sender, EventArgs e)
        {

            merchant = merchantDal.Querybyid(merchantid);
            if (stype == "2")
            {
                activity = merchant_activityDal.Querybyactid(actvid);//查看活动详情
                dpstart.Value = Convert.ToDateTime(activity.start_time);//活动开始时间
                dpend.Value= Convert.ToDateTime(activity.end_time);//活动结束时间
                txtgoodname.Text = activity.goods_name;//商品名称
                txtdescribes.Text = activity.describes;//商品介绍
                txtmarkprice.Text = activity.market_price;//价格
                txtprice.Text = activity.discount_price;//活动价格
                txtstock.Text = activity.stock;//库存
                                               //if (!string.IsNullOrEmpty(activity.specs))
                                               //{
                                               //    splist = JsonConvert.DeserializeObject<List<specs>>(good.specs);

                //    if (splist.Count > 0)
                //    {
                //        lblspecs.Text = splist[0].key + ":" + splist[0].value + "....";
                //    }
                //}
                Array arr = activity.carousel.Split(',');
                imgcontrol con = new imgcontrol();
                con.cover = @"C:\fbimage\" + activity.cover;
                con.index = 0;
                con.stype = "2";
                panimg.Controls.Add(con);
                imglist.Add(activity.cover);
                for (int i = 0; i < arr.Length; i++)
                {
                    imglist.Add(arr.GetValue(i).ToString());
                    int index = imglist.Count - 1;
                    imgcontrol con1 = new imgcontrol();
                    con1.cover = @"C:\fbimage\" + arr.GetValue(i).ToString();
                    con1.index = index;
                    con1.stype = "1";
                    panimg.Controls.Add(con1);
                }
                if (activity.u_sage == "1")
                {
                    usage = "1";
                    btnusage1.ForeColor = System.Drawing.Color.FromArgb(76, 151, 230);
                    btnusage1.BackColor = System.Drawing.Color.FromArgb(110, 219, 242);
                    btnusage2.BackColor = System.Drawing.Color.FromArgb(194, 194, 194);
                    btnusage2.ForeColor = System.Drawing.Color.DimGray;
                }
                else
                {
                    usage = "2";
                    btnusage2.ForeColor = System.Drawing.Color.FromArgb(76, 151, 230);
                    btnusage2.BackColor = System.Drawing.Color.FromArgb(110, 219, 242);
                    btnusage1.BackColor = System.Drawing.Color.FromArgb(194, 194, 194);
                    btnusage1.ForeColor = System.Drawing.Color.DimGray;
                }
            }
           // lblstarttime.Text = DateTime.Now.ToString("yyyy/MM/dd");
        }
        /// <summary>
        /// 到店
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnusage1_Press(object sender, EventArgs e)
        {
            usage = "1";
            btnusage1.ForeColor = System.Drawing.Color.FromArgb(76, 151, 230);
            btnusage1.BackColor = System.Drawing.Color.FromArgb(110, 219, 242);
            btnusage2.BackColor = System.Drawing.Color.FromArgb(194, 194, 194);
            btnusage2.ForeColor = System.Drawing.Color.DimGray;
            btnusage3.BackColor = System.Drawing.Color.FromArgb(194, 194, 194);
            btnusage3.ForeColor = System.Drawing.Color.DimGray;
        }
        /// <summary>
        /// 物流
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnusage2_Press(object sender, EventArgs e)
        {
            //Toast("暂未开放");
            usage = "2";
            btnusage2.ForeColor = System.Drawing.Color.FromArgb(76, 151, 230);
            btnusage2.BackColor = System.Drawing.Color.FromArgb(110, 219, 242);
            btnusage1.BackColor = System.Drawing.Color.FromArgb(194, 194, 194);
            btnusage1.ForeColor = System.Drawing.Color.DimGray;
            btnusage3.BackColor = System.Drawing.Color.FromArgb(194, 194, 194);
            btnusage3.ForeColor = System.Drawing.Color.DimGray;
        }
        /// <summary>
        /// 外卖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnusage3_Press(object sender, EventArgs e)
        {
            Toast("暂未开放");
            //usage = "3";
            //btnusage3.ForeColor = System.Drawing.Color.FromArgb(76, 151, 230);
            //btnusage3.BackColor = System.Drawing.Color.FromArgb(110, 219, 242);
            //btnusage2.BackColor = System.Drawing.Color.FromArgb(194, 194, 194);
            //btnusage2.ForeColor = System.Drawing.Color.DimGray;
            //btnusage1.BackColor = System.Drawing.Color.FromArgb(194, 194, 194);
            //btnusage1.ForeColor = System.Drawing.Color.DimGray;
        }
        /// <summary>
        /// 规格设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel5_Press(object sender, EventArgs e)
        {
            SpecsFrom frm = new SpecsFrom();
            frm.splist = splist;
            this.Form.Show(frm, (obj, args) =>
            {
                splist = frm.splist;
                if (splist.Count > 0)
                {
                    lblspecs.Text = splist[0].key + ":" + splist[0].value + "....";
                }
            });
        }

        private void txtgoodname_TouchLeave(object sender, EventArgs e)
        {
            //Regex rg = new Regex("^[\u4e00-\u9fa5]$");  //正则表达式
            //if (!rg.IsMatch(txtgoodname.Text))
            //{


            //}
            txtgoodname.Text = Regex.Replace(txtgoodname.Text, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", "");
        }

        private void txtdescribes_TouchLeave(object sender, EventArgs e)
        {
            txtdescribes.Text = Regex.Replace(txtdescribes.Text, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", "");
        }
    }
}